<template>
    <div class="company-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="公司名称" prop="name">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.name"
                        clearable
                        @keyup.enter="getLists"
                    />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-select v-model="queryParams.status" class="w-[280px]" clearable>
                        <el-option label="全部" value="" />
                        <el-option label="正常" :value="1" />
                        <el-option label="禁用" :value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="getLists">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                    <el-button type="primary" @click="handleAdd()">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        新增公司
                    </el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-table
                ref="tableRef"
                class="mt-4"
                size="large"
                v-loading="pager.loading"
                :data="pager.lists"
            >
                <el-table-column type="selection" width="55" />
                <el-table-column label="公司信息" min-width="200">
                    <template #default="{ row }">
                        <div>ID: {{ row.id }}</div>
                        <div>名称: {{ row.company_name }}</div>
                        <div>标识: {{ row.company_id }}</div>
                        <div>城市: {{ row.city_name }}</div>
                        <div>区域编码: {{ row.area_code }}</div>
                    </template>
                </el-table-column>
                <el-table-column label="距离配置" min-width="150">
                    <template #default="{ row }">
                        <div>起点最小: {{ row.min_distance_departure / 1000 }}公里</div>
                        <div>起点最大: {{ row.max_distance_departure / 1000 }}公里</div>
                        <div>终点最小: {{ row.min_distance_destination / 1000 }}公里</div>
                        <div>终点最大: {{ row.max_distance_destination / 1000 }}公里</div>
                    </template>
                </el-table-column>
                <el-table-column label="状态" min-width="120">
                    <template #default="{ row }">
                        <div>状态: {{ row.status === 1 ? '正常' : '禁用' }}</div>
                        <div>定位点上传: {{ row.position_upload === 1 ? '是' : '否' }}</div>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="280" fixed="right">
                    <template #default="{ row }">
                        <div class="flex flex-col items-start">
                            <div class="flex gap-2 mb-2">
                                <!-- <el-button type="primary" link @click="handleDispatchConfig(row)">
                                    运价配置
                                </el-button> -->
                                <el-button type="primary" link @click="handleDataConfig(row)">
                                    数据上传配置
                                </el-button>
                            </div>
                            <div class="flex gap-2 mb-2">
                                <el-button type="primary" link @click="handleManualOrder(row)">
                                    手动发单
                                </el-button>
                                <el-button type="primary" link @click="handleRuleSettings(row)">
                                    规则设置
                                </el-button>
                            </div>
                            <div class="flex gap-2 mb-2">
                                <el-button type="primary" link @click="handleViewCar(row)">
                                    查看司机
                                </el-button>
                                <el-button type="primary" link @click="handleEdit(row)">
                                    编辑
                                </el-button>
                            </div>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
        <upload-config-popup v-if="showUploadConfig" ref="uploadConfigRef" @close="showUploadConfig = false" />
        <manual-order-popup v-if="showManualOrder" ref="manualOrderRef" @close="showManualOrder = false" />
        <price-config-popup v-if="showPriceConfig" ref="priceConfigRef" @close="showPriceConfig = false" />
    </div>
</template>

<script lang="ts" setup name="company">
import type { ElTable, FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus'
import EditPopup from './edit.vue'
import ManualOrderPopup from './manual-dispatch.vue'
import UploadConfigPopup from './upload-config.vue'
import PriceConfigPopup from './price-config.vue'
import { companyLists } from '@/api/supervise/company'
import { usePaging } from '@/hooks/usePaging'

const tableRef = shallowRef<InstanceType<typeof ElTable>>()
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const uploadConfigRef = shallowRef<InstanceType<typeof UploadConfigPopup>>()
const manualOrderRef = shallowRef<InstanceType<typeof ManualOrderPopup>>()
const priceConfigRef = shallowRef<InstanceType<typeof PriceConfigPopup>>()

const showEdit = ref(false)
const showUploadConfig = ref(false)
const showManualOrder = ref(false)
const showPriceConfig = ref(false)

const formRef = shallowRef<FormInstance>()
const showRuleSettings = ref(false)

const queryParams = reactive({
    name: '',
    status: ''
})

// 使用 usePaging hook
const { pager, getLists,resetParams } = usePaging({
    fetchFun: companyLists,
    params: queryParams
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (row: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(row)
}

// 其他操作方法
const handleManualOrder = async (row: any) => {
    // 实现手动发单逻辑
    showManualOrder.value = true
    await nextTick()
    manualOrderRef.value?.show(row)
}

const handleViewCar = (row: any) => {
    // 实现查看司机逻辑
}

const handleRuleSettings = (row: any) => {
    // 实现规则设置逻辑
}

const handleDataConfig = async (row: any) => {
    // 实现数据上传配置逻辑
    showUploadConfig.value = true
    await nextTick()
    uploadConfigRef.value?.show(row)
}

const handleDispatchConfig = (row: any) => {
    // 实现运价配置逻辑
}

onMounted(() => {
    getLists()
})
</script>